package cn.steven.player.control

import android.content.Context
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource


class ExoMediaSourceHelper(context: Context) {
    fun getMediaSource(path: String, headers: Map<String?, String?>?): MediaSource {
        if (path.endsWith(".m3u8")) {
            return createHlsSource(path)
        }
        return createProgressiveMediaSource(path)
    }

    private fun createProgressiveMediaSource(path: String): MediaSource {
        val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
        return ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(path))
    }

    private fun createHlsSource(path: String): MediaSource {
        val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
        return HlsMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(path))
    }


}
